<template>
  <div class="inheritor-detail" v-if="inheritor">
    <el-dialog
        v-model="showChat"
        :title="currentInheritor ? `与${currentInheritor.name}交流` : '非遗咨询'"
        width="60%"
        @closed="handleCloseChat"
    >
      <ChatWindow
          v-if="currentInheritor"
          :inheritor="currentInheritor"
      />
    </el-dialog>
    <!-- 返回按钮 -->
    <button class="back-button" @click="$router.go(-1)">
      <i class="fas fa-arrow-left"></i> 返回列表
    </button>

    <!-- 传承人基本信息 -->
    <div class="detail-header">
      <div class="profile-image">
        <img :src="inheritor.image" :alt="inheritor.name">
      </div>
      <div class="profile-info">
        <h1 class="clickable-name" @click="openChat(inheritor)">
          {{ inheritor.name }}
          <el-tag type="warning" size="small">在线</el-tag>
        </h1>
        <div class="meta-info">
          <span class="category">{{ getCategoryName(inheritor.category) }}</span>
          <span class="region">{{ inheritor.region }}</span>
          <span class="age">{{ inheritor.age }}岁</span>
        </div>
        <p class="brief">{{ inheritor.brief }}</p>
      </div>
    </div>

    <!-- 详情内容 -->
    <div class="detail-content">
      <div class="content-section">
        <h2>传承项目介绍</h2>
        <p>{{ inheritor.projectDescription }}</p>
      </div>

      <div class="content-section">
        <h2>传承人经历</h2>
        <p>{{ inheritor.experience }}</p>
      </div>

      <div class="content-section">
        <h2>成就与荣誉</h2>
        <ul class="achievements">
          <li v-for="(achievement, index) in inheritor.achievements" :key="index">
            {{ achievement }}
          </li>
        </ul>
      </div>

      <!-- 图片展示 -->
      <div class="gallery" v-if="inheritor.gallery && inheritor.gallery.length">
        <h2>相关图片</h2>
        <div class="gallery-grid">
          <div
              class="gallery-item"
              v-for="(image, index) in inheritor.gallery"
              :key="index"
              @click="openLightbox(index)"
          >
            <img :src="image" :alt="'传承人图片-' + index">
          </div>
        </div>
      </div>

      <!-- 视频展示 -->
      <div class="video-section" v-if="inheritor.video">
        <h2>相关视频</h2>
        <div class="video-container">
          <iframe
              :src="inheritor.video"
              frameborder="0"
              allowfullscreen
          ></iframe>
        </div>
      </div>
    </div>

    <!-- 图片灯箱 -->
    <div class="lightbox" v-if="showLightbox" @click.self="closeLightbox">
      <div class="lightbox-content">
        <button class="close-btn" @click="closeLightbox">
          <i class="fas fa-times"></i>
        </button>
        <img :src="inheritor.gallery[currentImageIndex]" :alt="'传承人图片-' + currentImageIndex">
        <button class="nav-btn prev" @click.stop="prevImage">
          <i class="fas fa-chevron-left"></i>
        </button>
        <button class="nav-btn next" @click.stop="nextImage">
          <i class="fas fa-chevron-right"></i>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { fetchInheritorById } from '@/api/inheritorApi.js'
import ChatWindow from '@/components/ChatWindow.vue'

export default {
  name: 'InheritorDetailView',
  components: {
    ChatWindow
  },
  setup() {
    const showChat = ref(false)
    const currentInheritor = ref(null)
    const inheritor = ref(null)

    const openChat = (inheritorData) => {
      currentInheritor.value = {
        id: inheritorData.id,
        name: inheritorData.name,
        avatar: inheritorData.image,
        category: inheritorData.category,
        online: true
      }
      showChat.value = true
    }
    const handleCloseChat = () => {
      currentInheritor.value = null
    }

    return {
      showChat,
      currentInheritor,
      inheritor,
      openChat,
      handleCloseChat
    }
  },
  data() {
    return {
      showLightbox: false,
      currentImageIndex: 0
    }
  },
  computed: {
    inheritorId() {
      return this.$route.params.id
    }
  },
  async created() {
    const data = await fetchInheritorById(this.inheritorId);
    const baseUrl = 'http://localhost:8089/images/';

    this.inheritor = {
      ...data,
      image: baseUrl + data.image,
      achievements: data.achievements || [], // 直接赋值
      gallery: (data.gallery || []).map(img => baseUrl + img), // 拼接路径即可
      video: data.video
    };
  },


  methods: {
    getCategoryName(categoryId) {
      const categories = {
        'folk-literature': '民间文学',
      'traditional-music': '传统音乐',
      'traditional-dance':'传统舞蹈',
      'traditional-drama': '传统戏剧',
          'quyi': '曲艺',
      'traditional-sports': '传统体育',
          'recreation-acrobatics': '游艺与杂技',
     ' traditional-art': '传统美术',
     ' traditional-craft': '传统技艺',
      'traditional-medicine': '传统医药',
     ' folk-custom': '民俗'
      }

      return categories[categoryId] || categoryId
    },
    openLightbox(index) {
      this.currentImageIndex = index
      this.showLightbox = true
      document.body.style.overflow = 'hidden'
    },
    closeLightbox() {
      this.showLightbox = false
      document.body.style.overflow = 'auto'
    },
    prevImage() {
      this.currentImageIndex = (this.currentImageIndex - 1 + this.inheritor.gallery.length) % this.inheritor.gallery.length
    },
    nextImage() {
      this.currentImageIndex = (this.currentImageIndex + 1) % this.inheritor.gallery.length
    },
    openChat(inheritor) {
      this.currentInheritor = inheritor
      this.showChat = true
      this.$nextTick(() => {
        this.$store.commit('chat/SET_CURRENT_INHERITOR', inheritor)
      })
    },
    // 关闭聊天窗口
    handleCloseChat() {
      this.$store.commit('chat/CLEAR_HISTORY')
    }

  }
}
</script>

<style scoped>
.inheritor-detail {
  max-width: 1200px;
  margin: 0 auto;
  padding-top: 60px;
}

.back-button {
  padding: 8px 15px;
  background: #f5f5f5;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 20px;
  font-size: 0.9rem;
}

.back-button:hover {
  background: #e0e0e0;
}

.detail-header {
  display: flex;
  gap: 30px;
  margin-bottom: 40px;
}

.profile-image {
  flex: 0 0 300px;
  height: 350px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
}

.profile-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-info {
  flex: 1;
}

.profile-info h1 {
  margin: 0 0 15px 0;
  font-size: 2rem;
  color: #333;
}

.meta-info {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.meta-info span {
  padding: 4px 10px;
  background: #f0f0f0;
  border-radius: 4px;
  font-size: 0.9rem;
  color: #666;
}

.profile-info .brief {
  font-size: 1.1rem;
  line-height: 1.6;
  color: #444;
}

.detail-content {
  margin-top: 40px;
}

.content-section {
  margin-bottom: 40px;
}

.content-section h2 {
  color: #333;
  border-bottom: 2px solid #42b983;
  padding-bottom: 8px;
  margin-bottom: 20px;
}

.content-section p {
  line-height: 1.8;
  color: #555;
}

.achievements {
  padding-left: 20px;
}

.achievements li {
  margin-bottom: 10px;
  line-height: 1.6;
}

.gallery {
  margin-top: 50px;
}

.gallery h2 {
  color: #333;
  border-bottom: 2px solid #42b983;
  padding-bottom: 8px;
  margin-bottom: 20px;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 15px;
}

.gallery-item {
  height: 150px;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s;
}

.gallery-item:hover {
  transform: scale(1.03);
}

.gallery-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.video-section {
  margin-top: 50px;
}

.video-section h2 {
  color: #333;
  border-bottom: 2px solid #42b983;
  padding-bottom: 8px;
  margin-bottom: 20px;
}

.video-container {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 */
  height: 0;
  overflow: hidden;
}

.video-container iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
.clickable-name {
  cursor: pointer;
  transition: color 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.clickable-name:hover {
  color: #409eff;
  text-decoration: underline;
}

/* 调整对话框样式 */
:deep(.el-dialog__header) {
  background: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-dialog__title) {
  color: #333;
  font-size: 1.2rem;
}
/* 灯箱样式 */
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.lightbox-content {
  position: relative;
  max-width: 90%;
  max-height: 90%;
}

.lightbox-content img {
  max-width: 100%;
  max-height: 80vh;
  display: block;
  margin: 0 auto;
}

.close-btn {
  position: absolute;
  top: -40px;
  right: 0;
  background: none;
  border: none;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
}

.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.3);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
}

.nav-btn.prev {
  left: 20px;
}

.nav-btn.next {
  right: 20px;
}

@media (max-width: 768px) {
  .detail-header {
    flex-direction: column;
  }

  .profile-image {
    flex: 0 0 auto;
    height: 250px;
  }
}
</style>